<!--<style>-->
<!--.layui-btn{margin:3px;}-->
<!--</style>-->

<div style="margin-left: 94%;">
    <a class="layui-btn layui-btn-primary layui-btn-sm operate" operate-type="add"><i class="layui-icon">&#xe608;</i>添加</a>
</div>

<table class="layui-table">
    <thead>
    <tr>
        <th width="5%;">ID</th>
        <th width="10%;">封面</th>
        <th width="10%;">标题</th>
        <th width="10%;">简介</th>
        <th width="5%;">创建日期</th>
        <th width="10%;">操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<!--分页start-->
<div id="paginate"></div>
<!--分页end-->

<script>
    $(function(){
        GetTableList();
    });

    //获取表格数据
    function GetTableList(page=1,pagesize=10){
        var url = '/handler/news/getNewsList';
        var data = {page:page,pagesize:pagesize};
        dink.post(url,data,function(res){
            if(res.ret == 1){
                var target = $('tbody');
                var html = '';
                $.each(res.data,function(i,ele){
                    html += '<tr>\n' +
                        '    <td>'+(i+1)+'</td>\n' +
                        '    <td><image style="width:50px;height:50px;" src="'+ele.cover+'" /></td>\n' +
                        '    <td>'+ele.title+'</td>\n' +
                        '    <td>'+(ele.description ? ele.description.substr(0,10)+'...' : '')+'</td>\n' +
                        '    <td>'+dink.utd(ele.ctime)+'</td>\n' +
                        '    <td>\n' +
                        '        <a class="layui-btn layui-btn-xs operate" operate-type="edit" data-id="'+ele.id+'">编辑</a>\n' +
                        '        <a class="layui-btn layui-btn-xs operate" operate-type="delete" data-id="'+ele.id+'">删除</a>\n' +
                        '    </td>\n' +
                        '</tr>';
                });
                target.html(html);
                if(page == 1){
                    Paginate(res.total,pagesize);
                }
            }else{
                var target = $('tbody');
                var html = '<tr><td colspan="6">无数据</td></tr>';
                target.html(html);
                layer.msg(res.msg);return;
            }
        });
    }

    //分页
    function Paginate(count,pagesize){
        layui.use('laypage',function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'paginate',
                count: count,
                limit: pagesize,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj,first){
                    if(!first){
                        GetTableList(obj.curr,pagesize);
                    }
                }
            });
        });
    }

    //操作按钮click事件
    dink.click('.operate',function(){
        var operate_type = $(this).attr('operate-type');
        var id = $(this).attr('data-id');
        var root = $(this).attr('data-root');
        var nickname = $(this).attr('data-nickname');
        var role_id = $(this).attr('data-role_id');
        //判断权限
        dink.post('/handler/permission/checkOperatePermission',{permission:'permission:project:'+operate_type},function(res){
            if(res.ret == 1){
                AdminNewsOpreate(operate_type,id,root,nickname,role_id);
            }else{
                layer.msg(res.msg);return;
            }
        });
        //end
    });

    //添加/编辑新闻
    function AdminNewsOpreate(operate='add',id=0){
        if(operate == 'add'){
            dink.html('AdminNewsAdd','添加新闻',['100%','100%']);
        }else if(operate == 'edit'){
            dink.html('AdminNewsAdd','编辑新闻',['100%','100%']);
            dink.post('/handler/news/getNewsDetailById',{id:id},function(res){
                var item = res.data;
                $('input[name=title]').val(item.title);
                $('textarea[name=description]').val(item.description);
                $('input[name=cover]').val(item.cover);
                $('#preview').html('<image src="'+item.cover+'" style="width:50px;height:50px;" />');
                ue.ready(function(){ue.setContent(item.detail);});
            });
        }else if(operate == 'delete'){
            dink.alert('Tips','确定删除？',function(){
                dink.post('/handler/news/deleteNews',{id:id},function(res){
                    layer.msg(res.msg);
                    GetTableList();return;
                });
            });
        }else{
            layer.msg('操作类型异常');return;
        }
        //添加、编辑
        dink.click('#add',function(){
            var data = {};
            var base = {};
            if(operate == 'add'){
                var url = '/handler/news/addNews';
            }else if(operate == 'edit'){
                var url = '/handler/news/editNews';
                if(id == 0){
                    layer.msg('缺少记录id');return;
                }
                data.id = id;
            }else{
                layer.msg('操作类型异常');return;
            }
            base.title = $('input[name=title]').val();
            base.description = $('textarea[name=description]').val();
            base.cover = $('input[name=cover]').val();
            //百度ueditor
            base.detail = ue.getContent();
            //end
            data.base = base;
            dink.alert('Tips','确定'+(operate=='add' ? '添加' : '编辑')+'新闻？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });
    }
</script>